/*
 * Copyright (c) 2025 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include "core/components_ng/pattern/list/list_item_pattern.h"

#include "base/log/dump_log.h"
#include "base/memory/ace_type.h"
#include "base/utils/multi_thread.h"
#include "base/utils/utils.h"
#include "core/components_ng/pattern/list/list_item_group_layout_property.h"
#include "core/components/common/properties/color.h"
#include "core/components_ng/base/inspector_filter.h"
#include "core/components_ng/pattern/list/list_item_layout_algorithm.h"
#include "core/components_ng/pattern/list/list_item_layout_property.h"
#include "core/components_ng/pattern/list/list_pattern.h"
#include "core/components_ng/property/property.h"
#include "core/components_v2/inspector/inspector_constants.h"
#include "core/common/container.h"
#include "core/components_ng/property/measure_utils.h"

namespace OHOS::Ace::NG {

void ListItemPattern::OnAttachToFrameNodeMultiThread()
{
    // do nothing unsafe thread
}

void ListItemPattern::OnAttachToMainTreeMultiThread()
{
    auto host = GetHost();
    CHECK_NULL_VOID(host);
    if (listItemStyle_ == V2::ListItemStyle::CARD) {
        SetListItemDefaultAttributes(host);
    }
}
void ListItemPattern::CloseSwipeActionMultiThread(OnFinishFunc&& onFinishCallback)
{
    auto host = GetHost();
    CHECK_NULL_VOID(host);
    host->PostAfterAttachMainTreeTask([onFinishCallback, weak = WeakClaim(this)]() {
        auto pattern = weak.Upgrade();
        CHECK_NULL_VOID(pattern);
        pattern->onFinishEvent_ = onFinishCallback;
        pattern->ResetSwipeStatus(true);
    });
}

void ListItemPattern::SetListItemStyleMultiThread(V2::ListItemStyle style)
{
    auto host = GetHost();
    CHECK_NULL_VOID(host);
    if (listItemStyle_ == V2::ListItemStyle::NONE && style == V2::ListItemStyle::CARD) {
        host->PostAfterAttachMainTreeTask([style, weak = WeakClaim(this), weakNode = WeakClaim(RawPtr(host))]() {
            auto node = weakNode.Upgrade();
            CHECK_NULL_VOID(node);
            auto pattern = weak.Upgrade();
            CHECK_NULL_VOID(pattern);
            pattern->listItemStyle_ = style;
            pattern->SetListItemDefaultAttributes(node);
            pattern->InitListItemCardStyleForList();
        });
    }
}

} // namespace OHOS::Ace::NG
